package com.mkvld.appietijdenv2.databases;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class UserBudgetWithdrawalDatabase extends SQLiteOpenHelper {
	private static final String DB_NAME = "budgetwithdrawals";
	private static final int DB_VERSION = 2;
	
	private static final String TABLE_NAME = "withdrawals";
	private static final String KEY_YEAR = "year";
	private static final String KEY_PERIOD = "period";
	private static final String KEY_TYPE = "type";
	private static final String KEY_HOURS = "hours";
	private static final String[] KEYS = { KEY_YEAR, KEY_PERIOD, KEY_TYPE, KEY_HOURS };
	
	private static final String CREATE_STRING = "CREATE TABLE " + TABLE_NAME
				+ " (" + KEY_YEAR + " INTEGER, " 
			+ KEY_PERIOD + " INTEGER, " 
			+ KEY_TYPE + " INTEGER, "
			+ KEY_HOURS + " TEXT)";
	
	private static final String selection = KEY_YEAR + " = ? AND " + KEY_PERIOD + " = ? AND " + KEY_TYPE + " = ?";
	
	
	public static final int TYPE_VACATION = 0;
	public static final int TYPE_ADV = 1;
	public static final int TYPE_TVT = 2;
	public static UserBudgetWithdrawalDatabase wDb;
	
	public UserBudgetWithdrawalDatabase(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_STRING);
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}
		
	
	public double getVacationWithdrawal(int year, int period) {
		return getWithdrawal(year, period, TYPE_VACATION);
	}
	
	public double getADVWithdrawal(int year, int period) {
		return getWithdrawal(year, period, TYPE_ADV);
	}
	
	public double getTVTWithdrawal(int year, int period) {
		return getWithdrawal(year, period, TYPE_TVT);
	}
	
	private double getWithdrawal(int year, int period, int type) {
		SQLiteDatabase db = getReadableDatabase();
		String[] selectionArgs = {
				Integer.toString(year),
				Integer.toString(period),
				Integer.toString(type)
		};
		Cursor cursor = db.query(TABLE_NAME, KEYS, selection, selectionArgs, null, null, null);
		double withdrawal = -1;
		if (cursor.moveToFirst()) {
			withdrawal = Double.parseDouble(cursor.getString(3));
		}
		cursor.close();
		
		return withdrawal;
	}
	
	public void addVacationWithdrawal(int year, int period, double hours) {
		addWithdrawal(year, period, TYPE_VACATION, hours);
	}
	
	public void addADVWithdrawal(int year, int period, double hours) {
		addWithdrawal(year, period, TYPE_ADV, hours);
	}
	
	public void addTVTWithdrawal(int year, int period, double hours) {
		addWithdrawal(year, period, TYPE_TVT, hours);
	}
	
	private void addWithdrawal(int year, int period, int type, double hours) {
		ContentValues cv = new ContentValues();
		cv.put(KEY_YEAR, year);
		cv.put(KEY_PERIOD, period);
		cv.put(KEY_TYPE, type);
		cv.put(KEY_HOURS, Double.toString(hours));
		
		SQLiteDatabase db = getWritableDatabase();
		db.insert(TABLE_NAME, null, cv);
		db.close();
	}
	
	public void deleteVacationWithdrawal(int year, int period) {
		deleteWithdrawal(year, period, TYPE_VACATION);
	}
	
	public void deleteADVWithdrawal(int year, int period) {
		deleteWithdrawal(year, period, TYPE_ADV);
	}
	
	public void deleteTVTWithdrawal(int year, int period) {
		deleteWithdrawal(year, period, TYPE_TVT);
	}
	
	private void deleteWithdrawal(int year, int period, int type) {
		String[] selectionArgs = {
				Integer.toString(year),
				Integer.toString(period),
				Integer.toString(type)
		};
		SQLiteDatabase db = getWritableDatabase();
		db.delete(TABLE_NAME, selection, selectionArgs);
	}
	
	
}
